package structure;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Sector implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final int SIZE = 100;
	
	private final int[] position;
	private long lastAccess;
	private byte[][][] data;
	
	public Sector(int[] position){
		this.position = position;
		data = new byte[SIZE][SIZE][SIZE];
	}
	
	public byte getBlock(int[] coord ){
		return data[coord[0]-position[0]][coord[1]-position[1]][coord[2]-position[2]];
	}
	public void setBlock(int[] coord, byte block){
	
		data[coord[0]-position[0]][coord[1]-position[1]][coord[2]-position[2]]=block;
	}
	
	public long getLastAccress(){
		return lastAccess;
	}
	
	public void setData(byte[][][] data){
		this.data = data;
	}
	
	public static Sector readSector(String fileName) throws Exception{
		FileInputStream fis = new FileInputStream(fileName);
		GZIPInputStream gs = new GZIPInputStream(fis);
		ObjectInputStream ois = new ObjectInputStream(gs);
		return (Sector)ois.readObject();
	}
	
	public void writeSector(String fileName) throws Exception{
		FileOutputStream fos = new FileOutputStream(fileName);
		GZIPOutputStream gz = new GZIPOutputStream(fos);
		ObjectOutputStream oos = new ObjectOutputStream(gz);
		oos.writeObject(this);
		oos.flush();
		oos.close();
		fos.close();
	}
}
